package mbsl.ui.dialogs;

import static mbsl.util.SWTUtils.Direction.*;
import mbsl.system.Core;
import mbsl.system.TextProvider;
import mbsl.util.SWTUtils;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.events.ShellAdapter;
import org.eclipse.swt.events.ShellEvent;
import org.eclipse.swt.layout.FormLayout;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;

public class Options {
	private static TextProvider tp = Core.tp;
	
	public Options(Shell pParent, String pTitle) {
		mWindow = new Shell(pParent, SWT.DIALOG_TRIM | SWT.APPLICATION_MODAL);
		
		mWindow.setLayout(new FormLayout());
		mWindow.setText(pTitle);
		mWindow.addShellListener(new ShellAdapter() {
			public void shellClosed(ShellEvent pE) {
				close();
			}
		});

		// Cancel button
		Button cancel = new Button(mWindow, SWT.PUSH);
		
		cancel.setLayoutData(SWTUtils.getFormData(
				SWTUtils.getMeta(down, 100, -2),
				SWTUtils.getMeta(right, 100, -2)
		));
		cancel.setText(tp.get("ui.cancel"));
		cancel.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(SelectionEvent pE) {
				close();
			}
		});
		// OK button
		mConfirm = new Button(mWindow, SWT.PUSH);
		
		mConfirm.setLayoutData(SWTUtils.getFormData(cancel.computeSize(SWT.DEFAULT, SWT.DEFAULT).x, SWT.DEFAULT, 
				SWTUtils.getMeta(down, 100, -2),
				SWTUtils.getMeta(right, cancel, -2)
		));
		mConfirm.setText(tp.get("ui.ok"));
		// Component panel
		mPanel = new Composite(mWindow, SWT.NONE);
		
		GridLayout layout = new GridLayout();
		
		layout.numColumns = 2;
		
		mPanel.setLayoutData(SWTUtils.getFormData(
				SWTUtils.getMeta(up, 0, 0),
				SWTUtils.getMeta(left, 0, 0),
				SWTUtils.getMeta(right, 100, 0),
				SWTUtils.getMeta(down, mConfirm, 0)
		));
		mPanel.setLayout(layout);
	}
	
	public Button addCheckBox(String pLabel) {
		addLabel(pLabel);
		
		Button checkBox = new Button(mPanel, SWT.CHECK);
		
		checkBox.setLayoutData(new GridData(SWT.BEGINNING, SWT.CENTER, false, false));
		
		return checkBox;
	}
	/** Adds a label with the provided text.  The text is right-aligned by default. */
	protected Label addLabel(String pText) {
		Label label = new Label(mPanel, SWT.TRAIL);
		
		label.setLayoutData(new GridData(SWT.END, SWT.CENTER, false, false));
		label.setText(pText);
		
		return label;
	}
	
	/**
	 * Opens the dialog.
	 * <p>
	 * Note that this method will block until the dialog is closed
	 * @return The response, or null if the dialog was cancelled or closed.
	 */
	public void open() {
		mWindow.pack();

		mWindow.setVisible(true);
		
		SWTUtils.block(mWindow);
	}
	
	public void close() {
		mWindow.dispose();
	}
	
	public void addConfirmListener(SelectionListener pListener) {
		mConfirm.addSelectionListener(pListener);
	}
	
	private Button mConfirm;
	private Composite mPanel;
	private Shell mWindow;
}
